package com.jci.vsd.network.control;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.jci.vsd.application.VsdApplication;
import com.jci.vsd.bean.feedback.FeedBackBean;
import com.jci.vsd.bean.feedback.FeedBackDetailRequestBean;
import com.jci.vsd.bean.feedback.FeedBackDetailResponseBean;
import com.jci.vsd.bean.feedback.FeedBackReplyRequestBean;
import com.jci.vsd.bean.feedback.FeedBackRequestBean;
import com.jci.vsd.bean.feedback.FeedBackResponseBean;
import com.jci.vsd.bean.feedback.SubmitFeedBackRequestBean;
import com.jci.vsd.bean.feedback.UploadPhotoRequestBean;
import com.jci.vsd.bean.reim.ReimAddItemBean;
import com.jci.vsd.constant.AppConstant;
import com.jci.vsd.exception.IApiException;
import com.jci.vsd.network.api.FeedBackApi;
import com.jci.vsd.network.api.ReimApi;
import com.jci.vsd.network.api.UserInfoApi;
import com.jci.vsd.utils.BaseUtils;
import com.jci.vsd.utils.Loger;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by liqing on 17/11/20.
 */

public class FeedBackApiControl extends BaseControl {






//
//    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
//            .addFormDataPart("name", name)
//            .addFormDataPart("psd", psd)
//            .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
//            .build();

    public Observable<String> submitPic(final File file, final UploadProgressListener uploadProgressListener) {
        Retrofit retrofit = builderUploadPicRetrofit();
        FeedBackApi feedBackApi = retrofit.create(FeedBackApi.class);
        //application/octet-stream


        final ProgressRequestBody progressFileRequestBody = new ProgressRequestBody(file).create(MediaType.parse("application/octet-stream"), file, uploadProgressListener);
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        String fileName = android.os.Build.SERIAL + "_" + new Date().getTime() + "_" + file.getName();
        builder.addFormDataPart("original", fileName, progressFileRequestBody);
        //  builder.addFormDataPart("enctype", "multipart/form-data");//告诉das服务上传的是文件
        //  builder.addFormDataPart("userId", VsdApplication.getInstance().getLoginResponseBean().getId());
        builder.addFormDataPart("start", "12");
        builder.addFormDataPart("end", "1");

        builder.addFormDataPart("bp", "1");
        builder.addFormDataPart("dst", "1");
        builder.addFormDataPart("client", "1");//告诉das服务上传的是文件
        builder.addFormDataPart("amount", "1");
        builder.addFormDataPart("remark", "1");
        builder.addFormDataPart("number", "1");
        builder.addFormDataPart("qty", "1");

        builder.addFormDataPart("details", "1");
        builder.addFormDataPart("reason", "1");
        builder.addFormDataPart("cat", "1");
        builder.addFormDataPart("item", "1");


        return feedBackApi.upload(builder.build());
    }


    // single pic
    /*
      RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("original", file.getName(), requestBody);
        Loger.e("路径图片 " + file.getName());
     */

    //多图片

    public Observable<String> submitMultiPic2(List<File> files, final UploadProgressListener uploadProgressListener) {
        //  header 5
        // Retrofit retrofit = builderUploadPicRetrofit();
        Retrofit retrofit = builderUploadPicOneHeaderRetrofit();
        FeedBackApi api = retrofit.create(FeedBackApi.class);

        MultipartBody.Builder builder = new MultipartBody.Builder();

        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
            builder.addFormDataPart("files", file.getName(), requestBody);
        }
        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();


        return api.uploadFilesWithParts2(multipartBody);
    }


    //提交信息反馈
    public Observable<String> submitFeedBackContent(final FeedBackBean f) {
        Retrofit retrofit = builderJsonRetrofit();
        String params = new Gson().toJson(f);
        FeedBackApi feedBackApi = retrofit.create(FeedBackApi.class);
        Loger.i("submitFeedBackContentParams ---- " + params);
        Observable<Response<String>> observable = feedBackApi.submitFeedBackContent(params);


        return observable.map(new Function<Response<String>, String>() {
            @Override
            public String apply(Response<String> stringResponse) throws Exception {
                int httpCode = stringResponse.code();
                if(httpCode==200){
                    return stringResponse.body();
                }
                throw new IApiException(String.valueOf(httpCode), stringResponse.message());
            }
        });
    }



}
